#include "homeroot.h"
#include "stuitem.h"
#include "ui_stuitem.h"
#include "database.h"
#include "widget.h"
#include "ui_homeroot.h"
#include <QString>
#include <QStatusBar>
#include <QLabel>
#include <QDebug>
#include <QPushButton>
#include "stuitem.h"
#include "teaitem.h"
#include "scoitem.h"

HomeRoot::HomeRoot(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomeRoot)
{
    ui->setupUi(this);
    connect(ui->btn_logOut,&QPushButton::clicked,this,[=](){
        this->close();
        Widget * father= new Widget();
        father->show();
    });
    connect(ui->btn_select_stu,&QPushButton::clicked,this,[=](){
        on_btn_select_stu_clicked();
    });
    connect(ui->btn_select_tea,&QPushButton::clicked,this,[=](){
        on_btn_select_tea_clicked();
    });
    connect(ui->btn_select_sco,&QPushButton::clicked,this,[=](){
        on_btn_select_sco_clicked();
    });
    connect(ui->btn_select_cou,&QPushButton::clicked,this,[=](){
       on_btn_select_cou_clicked();
    });
    connect(ui->btn_select_cla,&QPushButton::clicked,this,[=](){
        on_btn_select_cla_clicked();
    });
    connect(ui->btn_select_user,&QPushButton::clicked,this,[=](){
        on_btn_select_user_clicked();
    });
}

QString HomeRoot::inital(QString id,QString name)
{
    HomeRoot::id = id.toUtf8();
    HomeRoot::name = name.toUtf8();
    //更新root主页显示
    ui->lableUserinfo->setText("欢迎:"+HomeRoot::name+" \nID:  "+HomeRoot::id);
}

HomeRoot::~HomeRoot()
{
    delete ui;
}

void HomeRoot::on_btn_select_stu_clicked()
{
    ui->listWidget->clear();
    QSqlQuery query;
    QString selectStudent = QString("select s_id,s_name,s_sex,s_phone,s_addr from t_student");
    if(query.exec(selectStudent))
    {
        while(query.next())
        {
            QString id = query.value("s_id").toString();
            QString name = query.value("s_name").toString();
            QString sex = query.value("s_sex").toString();
            QString phone = query.value("s_phone").toString();
            QString addr = query.value("s_addr").toString();
            StuItem *stu = new StuItem;
            stu->setStuInfo(id,name,sex,phone,addr);
            QListWidgetItem *item = new QListWidgetItem;
            item->setSizeHint(QSize(700,60));
            ui->listWidget->addItem(item);
            ui->listWidget->setItemWidget(item,stu);
        }
    }else{
        qDebug()<<"error";
    }
}

void HomeRoot::on_btn_select_tea_clicked()
{
    ui->listWidget->clear();
    QSqlQuery query;
    QString selectStudent = QString("select * from t_teacher");
    if(query.exec(selectStudent))
    {
        while(query.next())
        {
            QString id = query.value("t_no").toString();
            QString name = query.value("t_name").toString();
            QString course = query.value("t_co_id").toString();
            TeaItem *tea = new TeaItem;
            tea->setTeaInfo(id,name,course);
            QListWidgetItem *item = new QListWidgetItem;
            item->setSizeHint(QSize(700,60));
            ui->listWidget->addItem(item);
            ui->listWidget->setItemWidget(item,tea);
        }
    }else{
        qDebug()<<"error";
    }
}

void HomeRoot::on_btn_select_sco_clicked()
{
    ui->listWidget->clear();
    QSqlQuery query;
    QString selectScore = QString("select * from t_score");
    if(query.exec(selectScore))
    {
        while(query.next())
        {
            QString co_id = query.value("sc_co_id").toString();
            QString s_id = query.value("sc_s_id").toString();
            QString score = query.value("sc_score").toString();
            ScoItem * sco = new ScoItem;
            sco->setScoInfo(co_id,s_id,score);
            QListWidgetItem *item = new QListWidgetItem;
            item->setSizeHint(QSize(700,60));
            ui->listWidget->addItem(item);
            ui->listWidget->setItemWidget(item,sco);
        }
    }else{
        qDebug()<<"error";
    }
}

void HomeRoot::on_btn_select_cla_clicked()
{

}

void HomeRoot::on_btn_select_cou_clicked()
{

}

void HomeRoot::on_btn_select_user_clicked()
{

}
